package view;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.*;
import modle.*;
import util.*;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.Toolkit;

public class Admin_Register extends JFrame {

	private static JPanel contentPane;
	private JTextField UserName;
	private JTextField Email;
	private JPasswordField PassWord;
	private static JComboBox comboBox;
	private static List<Country> listCountries;
	private static Locale locale;
	private static ResourceBundle rd;
	private static JLabel lblEmail;
	private static JLabel lblPassword;
	private static JLabel lblAddNewAdmin;
	private static JLabel un_Label;
	private static JButton btnRegister;
	private JLabel backgroundlabel;
	private static JButton btnBack;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Admin_Register frame = new Admin_Register();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Admin_Register() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Admin_Register.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 479);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblAddNewAdmin = new JLabel("Add New Admin");
		lblAddNewAdmin.setForeground(new Color(205, 133, 63));
		lblAddNewAdmin.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		lblAddNewAdmin.setBounds(175, 23, 198, 38);
		contentPane.add(lblAddNewAdmin);
		
		UserName = new JTextField();
		UserName.setBounds(280, 100, 198, 31);
		contentPane.add(UserName);
		UserName.setColumns(10);
		
		Email = new JTextField();
		Email.setColumns(10);
		Email.setBounds(280, 245, 198, 31);
		contentPane.add(Email);
		
		PassWord = new JPasswordField();
		PassWord.setBounds(280, 173, 198, 31);
		contentPane.add(PassWord);
		
		un_Label = new JLabel("UserName: ");
		un_Label.setForeground(new Color(70, 130, 180));
		un_Label.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		un_Label.setBounds(129, 97, 137, 31);
		contentPane.add(un_Label);
		
		lblPassword = new JLabel("PassWord: ");
		lblPassword.setForeground(new Color(70, 130, 180));
		lblPassword.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		lblPassword.setBounds(129, 173, 137, 31);
		contentPane.add(lblPassword);
		
		lblEmail = new JLabel("Email: ");
		lblEmail.setForeground(new Color(70, 130, 180));
		lblEmail.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 21));
		lblEmail.setBounds(129, 245, 137, 31);
		contentPane.add(lblEmail);
		
		btnRegister = new JButton("Register");
		btnRegister.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 23));
		btnRegister.setBounds(221, 326, 168, 38);
		contentPane.add(btnRegister);
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				registerActionListener(e);
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					
					//rd到底有什么用
					
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(470, 13, 98, 32);
		contentPane.add(comboBox);
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnBack.setBounds(14, 382, 92, 38);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Admin_Menu().setVisible(true);
				dispose();
			}
		});
		
		fillCountries();
	}
	
	private ImageIcon ResizeImage(String imgpath, JLabel label) {
		// TODO Auto-generated method stub
		ImageIcon img1 = new ImageIcon(Admin_Register.class.getResource(imgpath));
		Image img2 = img1.getImage();
		Image img3 = img2.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon img4 = new ImageIcon(img3);
		return img4;
	}

	protected void registerActionListener(ActionEvent e) {
		// TODO Auto-generated method stub
		
		String username = UserName.getText();
		String password = new String(PassWord.getPassword());
		String email = Email.getText();
		
		if (StringUtil.isEmpty(username)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		if (StringUtil.isEmpty(email)) {
			JOptionPane.showMessageDialog(null, "邮箱不能为空！");
			return;
		}
		if (!StringUtil.checkEmail(email)) {
			JOptionPane.showMessageDialog(null, "邮箱格式错误！");
			return;
		}
		
		
		Admin user = new Admin(username, password, email);
		
		try {
			if(AdminDao.isUserExist(DbUtil.getCon(), user))
			{
				JOptionPane.showMessageDialog(null, "该管理账户已存在，可以直接输入账号密码来管理菜单");
				new Admin_Menu().setVisible(true);
				this.dispose();
				return;
			}
		}catch (Exception e1) {
			//弹出提示信息
			JOptionPane.showMessageDialog(null, "出现异常，注册失败！");
			e1.printStackTrace();
		}
		
		try {
			int num = AdminDao.Add(DbUtil.getCon(), user);
			if(num>0) {
				//弹出提示信息
				JOptionPane.showMessageDialog(null, "注册成功");
				new Admin_Menu().setVisible(true);
				this.dispose();
			}else {
				//弹出提示信息
				JOptionPane.showMessageDialog(null, "注册失败");
			}
		} catch (Exception e1) {
			//弹出提示信息
			JOptionPane.showMessageDialog(null, "出现异常，注册失败！");
			e1.printStackTrace();
		}
	}
	
	private static void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected static void changeLanguage() {
		// TODO Auto-generated method stub
		
		lblEmail.setText(rd.getString("lblEmail"));
		lblPassword.setText(rd.getString("lblPassword"));
		lblAddNewAdmin.setText(rd.getString("lblAddNewAdmin"));
		btnRegister.setText(rd.getString("btnRegister"));
		un_Label.setText(rd.getString("un_Label"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
